﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Budget.DataAccess.Repository;

namespace Budget.DataAccess.Services
{
	public class MthCategoryDataService : IMthCategoryDataService
	{
		private IRepository<MthCategory> repo;

		public MthCategoryDataService( IRepository<MthCategory> repository )
		{
			repo = repository;
		}

		#region IMthCategoryDataService Members

		public IList<MthCategory> GetAll()
		{
			IList<MthCategory> mthCats;

			using ( repo )
			{
				mthCats = repo.Query().ToList();
			}

			return mthCats;
		}

		public IList<MthCategory> GetByMonth( int MthId )
		{
			IList<MthCategory> mthCats;

			using ( repo )
			{
				mthCats = repo.Query().Where( mc => mc.MthId == MthId ).ToList();
			}

			return mthCats;
		}

		public void Save( MthCategory monthCategory )
		{
			using ( repo )
			{
				if ( monthCategory.MthCategoryId > 0 )
				{
					repo.Attach( monthCategory );
				}
				else
				{
					repo.Add( monthCategory );
				}

				repo.SubmitChanges();
			}
		}

		public void Delete( int MthCategoryId )
		{
			using ( repo )
			{
				MthCategory monthCategory = repo.Query().Where( mc => mc.MthCategoryId == MthCategoryId ).SingleOrDefault();

				if ( monthCategory != null )
				{
					repo.Remove( monthCategory );
					repo.SubmitChanges();
				}
			}
		}

		public MthCategory GetByMthCategoryId( int MthCategoryId )
		{
			MthCategory mthCat;

			using ( repo )
			{
				mthCat = repo.Query().Where( mc => mc.MthCategoryId == MthCategoryId ).SingleOrDefault();
			}

			return mthCat;
		}

		#endregion
	}
}
